<html>
<head><title>Clan War Map</title></head>

<?php 
require_once "../cw2conf.php";
include_once "../misc.php";

function TGroupIDFromName($tgroups, $name)
{
 foreach ($tgroups as $id => $tgroup)
 {
  if ($tgroup['name'] == $name)
  {
   return $id;
  }
 }
 if ($name = "Neutral") {return 0;};
 echo "<br/>Can't find group name '" . $name . "'<br/>";
 return 0;
}

function ShowMap($tournament_id, $dbl)
{
 $planet_icon_radius = 40;
 $planet_icon_type = 1;
 $fort_icon_radius = 30;
 $fort_icon_type = 2;
 $unit_group_icon_radius = 25;
 $unit_group_icon_type = 3;
 $min_radius = 50;
 
 $planet_size_texts = array("???", "Low", "Medium", "High");
 $planet_size_short_texts = array("", "Low", "Med", "High");
 $unit_type_texts = array('army' => "(Army)", 'garrison' => "(Garrison)");
 
 $tournament_id = 1;

 //  TOURNAMENT GROUPS
 $tgroups = array();
 $sql = "SELECT * FROM tm_groups WHERE tournament=" . $tournament_id . ";";
 if (!($tgroup_result = mysql_query($sql,$dbl)))
 {
  fecho("Select groups - query ", $sql, " failed : ", mysql_error());
  return;
 }
 while ($tgroup = mysql_fetch_assoc($tgroup_result))
 {
  $tgroups[$tgroup['id']] = $tgroup;
 }
 
 // ***
 $sql = "TRUNCATE TABLE `cw2_planets` ";
 mysql_query($sql,$dbl);
 $sql = "TRUNCATE TABLE `cw2_units` ";
 mysql_query($sql,$dbl);
 $sql = "TRUNCATE TABLE `tm_players` ";
 mysql_query($sql,$dbl);
 $sql = "TRUNCATE TABLE `players` ";
 mysql_query($sql,$dbl);
 // ***
 
 $planet_sizes = array('Med' => 2, 'Low' => 1, 'Hig' => 3);
 $planet_coords = array('NE1' => array(506, 100), 'NW1' => array(103, 70), 'NW2' => array(361, 41), 'NE1' => array(505, 101), 'NE5' => array(714, 159), 'NE3' => array(869, 121), 'NW3' => array(59, 164), 'NW4' => array(207, 159), 'NW5' => array(382, 194), 'NE4' => array(588, 236), 'NE6' => array(829, 236), 'SE3' => array(876, 354), 'SW1' => array(25, 335), 'SW2' => array(192, 348), 'Andromeda' => array(398, 316), 'SE1' => array(551, 377), 'SW3' => array(69, 438), 'SW4' => array(236, 497), 'SW5' => array(41, 644), 'SW6' => array(192, 605), 'SW7' => array(322, 583), 'SE4' => array(430, 579), 'SE6' => array(549, 651), 'SE7' => array(669, 571), 'SE8' => array(814, 608), 'NE2' => array(702, 32), 'SE2' => array(763, 470), 'SE5' => array(595, 444));
 
 // ***
 
 $f_str = file_get_contents ("D:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\CW2\\Tmp\\original_map2.php");
 $f_str = htmlspecialchars($f_str);
 $arr_fs = explode("system=",$f_str);
 unset($arr_fs[0]);
 
 $unit_count = 0;
 $tplayers_cache = array();
  
 foreach ($arr_fs as $planet_id=>$s)
 {
  $pos = strpos($s, '&quot;');
  $planet_name = substr($s, 0, $pos);
  
  $pos = strpos($s, '(');
  $s = substr($s, $pos+1);
  $planet_size = $planet_sizes[substr($s, 0, 3)];
  //echo "<br>planet_size=$planet_size<br>s=$s<br>";

  $pos = strpos($s, ',');
  $s = substr($s, $pos+1);
  $pos = strpos($s, ',');
  $map_name = trim(substr($s, 0, $pos));
  //echo "<br>map_name=$map_name<br>";
  
  $pos = strpos($s, ')');
  $s = substr($s, $pos+1);
  $pos = strpos($s, '&quot;');
  $tgroup_name = trim(substr($s, 0, $pos));
  $tgroup_id = TGroupIDFromName($tgroups, $tgroup_name);
  
  $planet_flags = 0;
  $pos = strpos($s, 'Fort');
  if ($pos !== false) {$planet_flags+= CW2PLANET_FORT;}
  
  $sql = "INSERT INTO `cw2_planets` ( `planet_id` , `tournament_id` , `planet_name` , `planet_size` , `tgroup_id` , `planet_x` , `planet_y` , `map_name`, `planet_flags`) VALUES ( '".$planet_id."', '".$tournament_id."', '".$planet_name."', '".$planet_size."', '".$tgroup_id."', '".$planet_coords[$planet_name][0]."', '".$planet_coords[$planet_name][1]."', '".$map_name."', '".$planet_flags."');";
  mysql_query($sql,$dbl);
  
  //echo "<br>s(4)=$s<br>";
  
  $arr_units = explode ('title=', $s);
  unset($arr_units[0]);
  
  foreach ($arr_units as $arr_units_id => $s2)
  {
   $pos = strpos($s2, 'Fort');
   if ($pos !== false) {continue;}
  
   //echo "<br>s2(1)=$s2<br>";
   $pos = strpos($s2, '&quot;&gt;');
   $s2 = substr($s2, 0, $pos);
   //echo "<br>s2(2)=$s2<br>";

   $pos = strpos($s2, 'title="');
   $s2 = substr($s2, $pos+6);
   $pos = strpos($s2, ':');
   $tgroup_name = trim(substr($s2, 0, $pos));
   $tgroup_id = TGroupIDFromName($tgroups, $tgroup_name);
   //echo "<br>tgroup_name=$tgroup_name tgroup_id=$tgroup_id <br>";
   
   $pos = strpos($s2, '---');
   $s2 = substr($s2, $pos+5);
   //echo "<br>s2(3)=$s2<br>";
   
   $arr_units2 = explode (',', $s2);
   foreach ($arr_units2 as $arr_units_id2 => $s3)
   {
    $unit_flags = 0;
    $pos = strpos($s3, 'Garrison');
    if ($pos !== false) {$unit_flags += CW2UNIT_GARRISON;}
    $pos = strpos($s3, '(');
    $player_name = trim(substr($s3, 0, $pos));
    echo "<br>player_name='$player_name' unit_flags=$unit_flags tgroup_name=$tgroup_name tgroup_id=$tgroup_id <br>";
    
    if (!IsSet($tplayers_cache[$player_name]))
    {
     $player_id = count($tplayers_cache) + 1;
     $tplayers_cache[$player_name] = array('id' => $player_id);
     $sql = "INSERT INTO `tm_players` ( `id` , `tournament` , `player_id` , `group1` , `comment` , `cw2_player_flags` )
VALUES ('".$player_id."', '".$tournament_id."', '".$player_id."', '".$tgroup_id."', NULL , '0');";
     mysql_query($sql,$dbl);
     $sql = "INSERT INTO `players` ( `id` , `alias` ) VALUES ('".$player_id."', '".$player_name."');";
     mysql_query($sql,$dbl);
    }
    $player_id = $tplayers_cache[$player_name]['id'];
    
    $unit_count++;
    
    $sql="INSERT INTO `cw2_units` ( `unit_id` , `tplayer_id` , `planet_id` , `unit_flags` , `tournament_id` )
VALUES ('".$unit_count."', '".$player_id."', '".$planet_id."', '".$unit_flags."', '".$tournament_id."');";
    mysql_query($sql,$dbl);
   }
  }
 }
 //print_r($arr_fs);
 
 return;
 
 //  PLANETS
 $planets = array();
 $sql = "SELECT * FROM cw2_planets WHERE tournament_id=".$tournament_id.";";
 if (!($planet_result = mysql_query($sql,$dbl)))
 {
  fecho("Select planets - query ", $sql, " failed : ", mysql_error());
  return;
 }
 while ($planet = mysql_fetch_assoc($planet_result))
 {
  $planet['d_icons'] = array();
  $planet['unit_groups'] = array();
  if ($planet_data['planet_size'] > 0)
  {
   $planet['d_icons'][] = array('r' => $planet_icon_radius, 'type' => $planet_icon_type, 'id' => $planet['planet_id']);
  }
  if ($planet_data['planet_flags'] & CW2PLANET_FORT == CW2PLANET_FORT)
  {
   $planet['d_icons'][] = array('r' => $fort_icon_radius, 'type' => $fort_icon_type, 'id' => $planet['planet_id']);
  }
  $planets[$planet_data['planet_id']] = $planet;
 }

 //  TOURNAMENT PLAYERS
 $sql = "SELECT tm_players.id, tm_players.group1, players.alias FROM tm_players, players WHERE tournament=" . $tournament_id . " AND tm_players.player_id = players.id;";
 if (!($tplayer_result = mysql_query($sql,$dbl))) 
 {
  fecho("Select tournament players - query ", $sql, " failed : ", mysql_error());
  return;
 }
 $tplayers = array();
 while ($tplayer = mysql_fetch_assoc($tplayer_result))
 {
  $tplayers[$tplayer['id']] = $tplayer;
 }
 
 //  UNITS
 $sql = "SELECT * FROM cw2_units WHERE tournament_id=".$tournament_id.";";
 if (!($unit_result = mysql_query($sql,$dbl)) )
 {
  fecho("Select units - query ", $sql, " failed : ", mysql_error());
  return;
 }
 $unit_groups = array();
 $units = array();
 while ($unit = mysql_fetch_assoc($unit_result))
 {
  $unit_id = $unit['unit_id'];
  $tplayer_id = $unit['tplayer_id'];
  $tgroup_id = $tplayers[$tplayer_id]['group1'];
  $planet_id = $unit['planet_id'];
  $unit_group_id = $tgroup_id . '_' . $planet_id;
  $is_garrison = 0;
  if ($unit['unit_flags'] & CW2UNIT_GARRISON == CW2UNIT_GARRISON)
  {
   $is_garrison = 1;
  }

  if (!IsSet($unit_groups[$unit_group_id]))
  {
   $unit_group[$unit_group_id] = array('units' => array($unit_id), 'tgroup_id' => $tgroup_id, 'num_armies' => (1 - $is_garrison), 'num_garrisons' => $is_garrison);
   $planets[$planet_id]['unit_groups'][] = $unit_group_id;
   $planets[$planet_id]['d_icons'][] = array('r' => $unit_group_icon_radius, 'type' => $unit_group_icon_type, 'id' => $unit_group_id, 'tgroup_id' => $tgroup_id);
  }
  else
  {
   $unit_group[$unit_group_id]['num_armies'] += (1 - $is_garrison);
   $unit_group[$unit_group_id]['num_garrisons'] += $is_garrison;
   
   $unit_groups[$unit_group_id]['units'][] = $unit_id;
  }
  
  $units[$unit_id] = $unit;
 }
 
 foreach ($planets as $planet_id => $planet)
 {
  $num_icons = count($planet['d_icons']);
  if ($num_icons == 0)
  {
   continue;
  }

  $circuit = 0;
  foreach ($planet['d_icons'] as $d_icon)
  {
   $circuit += $d_icon['r'];
  }
  if ($circuit == 0)
  {
   continue;
  }
  
  $radius = $circuit / pi() / 2;
  $circuit_multiplier = 1;
  if ($radius < $min_radius)
  {
   $circuit_multiplier = $min_radius / $radius;
   $circuit = $min_radius * pi() * 2;
   $radius = $min_radius;
  }
  
  $curr_circuit = 0;
  foreach ($planet['d_icons'] as $d_icon_idx => $d_icon)
  {
   //$angle = pi() * 2 * $d_icon_idx / $num_icons; // this one distributes evenly
   if ($d_icon_idx > 0)
   {
    $curr_circuit += $d_icon['r'] / 2;
   }
   $angle = ($curr_circuit * $circuit_multiplier) / $circuit; // more space for bigger icons
   $coord_x = round($planet['planet_x'] + $radius * sin ($angle));
   $coord_y = round($planet['planet_y'] + $radius * cos ($angle)); 
   $curr_circuit += $d_icon['r'] / 2;
   
   if ($d_icon['type'] == $unit_group_icon_type)
   {
    $unit_group = $unit_groups[$d_icon['id']];
    
    $tgroup_id = $unit_group['tgroup_id'];
    $tgroup_color = "#" . ColorToHex($tgroups[$tgroup_id]['color']);
    $tgroup_name = $tgroups[$tgroup_id]['name'];
    $tgroup_logo_url = $tgroups[$tgroup_id]['group_logo_url'];

    echo '<div id="icon" style="border: 1px solid ' . $tgroup_color . '; color: white; background-color: black; font-family: arial;
			font-size: 8pt; position: absolute; width: 25px; left:' . $coord_x . '; top:' . $coord_y . '" ';
			 echo 'title="' . $tgroup_name . ':<br/> --- <br/>';
    foreach ($unit_group['units'] as $unit_id)
    {
     $tplayer_name = $tplayers[$units[$unit_id]['tplayer_id']]['alias'];
     if ($units[$unit_id]['unit_flags'] & CW2UNIT_GARRISON == CW2UNIT_GARRISON)
     {
      $unit_type_text = $unit_type_texts['garrison'];
     } else {
      $unit_type_text = $unit_type_texts['army'];
     }
     echo $tplayer_name . ' ' . $unit_type_text . '<br/>';
    }
    echo '">';
    
    echo '<span style=" width: 25px; text-align: center"><img src="' . $tgroup_logo_url . '"></span><br/>';
			 echo '<div style="width: 25px; text-align: center; ">' . $unit_group['num_armies'] . '/' . $unit_group['num_garrisons'] . '</div></div>';

   }
   else if ($d_icon['type'] == $planet_icon_type)
   {
    $planet = $planets[$d_icon['id']];
    
    $tgroup_id = $planet['tgroup_id'];
    $tgroup_color = "#" . ColorToHex($tgroups[$tgroup_id]['color']);
    $tgroup_name = $tgroups[$tgroup_id]['name'];
    $tgroup_logo_url = $tgroups[$tgroup_id]['group_logo_url'];
    
    $planet_size = $planet['planet_size'];
    if (($planet_size < count($planet_size_texts)) && ($planet_size > 0))
    {
     $planet_size_text = $planet_size_texts[$planet_size];
     $planet_size_short_text = $planet_size_short_texts[$planet_size];
    }
    else
    {
     $planet_size_text = $planet_size_texts[$planet_size] . '(' . $planet_size . ')';
     $planet_size_short_text = '(' . $planet_size . ')';
    }
    
    
    echo '<a href="./systembattle.php?tournament_id=' . $tournament_id . '&planet_id=' . $planet['planet_id'];
    echo '" style="color: ' . $tgroup_color . '">';
    echo '<div id="icon" style=" position: absolute; left:' . $coord_x . ';	top: ' . $coord_y . ' "title="' . $planet['planet_name'];
    echo ' (' . $planet['map_name'] . ', ' . $planet_size_text . ', ' . $planet['planet_map'];
    //echo , [MA only]   << hmm? #666
    echo ') ' . $tgroup_name . '><nobr>';
    echo '<img src="' . $tgroup_logo_url . '" style="border: 2 solid ' . $tgroup_color . '; vertical-align: middle;">';
 		 echo '<span style="border: 2 solid ' . $tgroup_color . '; color: white; font-family: arial; font-size: 9pt; font-weight: bold;">';
    echo '&nbsp;' . $planet['planet_name'] . '&nbsp;(' . $planet_size_short_text . ')&nbsp;</span></nobr></div></a>';
   }
   else if ($d_icon['type'] == $fort_icon_type)
   {
		  echo '<div id="icon"style="position: absolute; left:' . $coord_x . '; top:' . $coord_y . '"title="Forces Of Redemption Fort">';
    echo '<img src="Logo/fort.gif"></div>';
   }
   else
   {
    fecho("Unknown icon type '" . $d_icon['type'] . "' - planet " . $planet['planet_id'] . ", icon idx " . $d_icon_idx);
   }
  }  
 }
}

$tournament_id = 0;
if (IsSet($_GET['tournament_id']))
{
 $tournament_id = IntVal($_GET['tournament_id']);
}

$dbl=DBConn();
ShowMap($tournament_id, $dbl);


?>

</body>
</html>
